<?php
namespace BP\Sys\XML;
require_once("java/io/BufferedReader.php");
require_once("java/io/File.php");
require_once("java/io/FileInputStream.php");
require_once("java/io/InputStreamReader.php");
require_once("java/util/ArrayList.php");
require_once("java/util/Hashtable.php");
require_once("java/util/List.php");
require_once("BP/DA/Cash.php");
require_once("BP/DA/DataColumn.php");
require_once("BP/DA/DataRow.php");
require_once("BP/DA/DataSet.php");
require_once("BP/DA/DataTable.php");
require_once("BP/DA/Depositary.php");
require_once("BP/DA/Log.php");
require_once("BP/En/Entities.php");
require_once("BP/En/Entity.php");
require_once("BP/En/Row.php");
abstract class XmlEns extends ArrayList {
	protected static $serialVersionUID;	// long
	public static function __staticinit() { // static class members
		self::$serialVersionUID = 1;
	}
	public static function convertXmlEns ($obj) // [Object obj]
	{
		return $obj;
	}
	public function ToJavaListXmlEns () 
	{
		return $this;
	}
	public function LoadXmlFile_String ($file) // [String file]
	{
			/* match: String_String */
		return $this->LoadXmlFile_String_String($file, $this->getTableName());
	}
	public function LoadXmlFile_String_String ($file, $table) // [String file, String table]
	{
		$ds = new DataSet();
		$ds->readXml($file);
		$dt = $ds->hashTables->get($table);
		foreach ($dt->Rows as $dr) 		{
			$en = $this->getNewEntity();
			$en->setRow(new Row());
			$en->getRow()->LoadDataTable($dt, $dr);
			$this->Add_XmlEn($en);
		}
		return $dt->Rows->size();
	}
	public function Contine ($key, $val) // [String key, String val]
	{
		foreach ($this as $en) 		{
			if ($en->GetValStringByKey($key)->equals($val))
			{
				return  TRUE ;
			}
		}
		return  FALSE ;
	}
	public static function constructor__ () 
	{
		$me = new self();
		return $me;
	}
	public function getTname () 
	{
		$tname = $this->getFile()->replace(".TXT", "")->replace(".txt", "");
		$tname = (($tname->substring(($tname->lastIndexOf("/") + 1)) . $this->getTableName()) . "_X");
		return $tname;
	}
	protected function GetTableTxts ($fis) // [List<File> fis]
	{
		$tempVar =Cash->GetObj($this->getTname(), $Depositary->Application);
		$cdt = (( (($tempVar instanceof DataTable)) ? $tempVar : NULL ));
		if (($cdt != NULL))
		{
			return $cdt;
		}
		$dt = new DataTable($this->getTableName());
		foreach ($fis as $fi) 		{
			/* match: DataTable_File */
			$dt = $this->GetTableTxt_DataTable_File($dt, $fi);
		}
		Cash->AddObj($this->getTname(), $Depositary->Application, $dt);
		return $dt;
	}
	protected function GetTableTxt () 
	{
		$tempVar =Cash->GetObj($this->getTname(), $Depositary->Application);
		$cdt = (( (($tempVar instanceof DataTable)) ? $tempVar : NULL ));
		if (($cdt != NULL))
		{
			return $cdt;
		}
		$dt = new DataTable($this->getTableName());
		$fi = new File($this->getFile());
			/* match: DataTable_File */
		$dt = $this->GetTableTxt_DataTable_File($dt, $fi);
		Cash->AddObj($this->getTname(), $Depositary->Application, $dt);
		return $dt;
	}
	protected function GetTableTxt_DataTable_File ($dt, $file) // [DataTable dt, File file]
	{
		$br = NULL;
		$fis = NULL;
		$isr = NULL;
		$ht = new Hashtable();
		try 
		{
			$fis = new FileInputStream($file);
			$isr = new InputStreamReader($fis, "UTF-8");
			$br = new BufferedReader($isr);
			$key = "";
			$val = "";
			$lin = "";
			while ((($lin = $br->readLine()) . NULL)) 
			{
				if ((($lin . NULL) . $lin->trim()->equals("")))
				{
					continue;
				}
				if (($lin->trim()->indexOf("*") == 0))
				{
					continue;
				}
				if (($lin->indexOf("=") == 0))
				{
					foreach ($ht->keySet() as $ojbkey) 					{
						if (!$dt->Columns->contains($ojbkey))
						{
							$dt->Columns->Add(new DataColumn($ojbkey, String));
						}
					}
					$dr = $dt->NewRow();
					foreach ($ht->keySet() as $ojbkey) 					{
						$dr->put($ojbkey, $ht->get($ojbkey));
					}
					if (($ht->keySet()->size() > 1))
					{
						$dt->Rows->add($dr);
					}
					$ht->clear();
					continue;
				}
				$idx = $lin->indexOf("=");
				if (($idx == -1))
				{
					throw new \RuntimeException(($this->getFile() . "@不符合规�? key =val 的规则�??"));
				}
				$key = $lin->substring(0, $idx);
				if ($key->equals(""))
				{
					continue;
				}
				$val = $lin->substring(($idx + 1));
				$ht->put($key, $val);
			}
		}
		catch (Exception $e)
		{
			$e->printStackTrace();
		}
		finally {
		{
			try 
			{
				$br->close();
				$isr->close();
				$fis->close();
			}
			catch (Exception $ex)
			{
				$ex->printStackTrace();
			}
		}
		}
		return $dt;
	}
	public function GetTable () 
	{
		$tempVar =Cash->GetObj($this->getTname(), $Depositary->Application);
		$cdt = (( (($tempVar instanceof DataTable)) ? $tempVar : NULL ));
		if (($cdt != NULL))
		{
			return $cdt;
		}
		if (($this->getFile()->toLowerCase()->indexOf(".txt") > 0))
		{
			return $this->GetTableTxt();
		}
		if (($this->getFile()->toLowerCase()->indexOf(".xml") > 0))
		{
			$ds1 = new DataSet();
			$ds1->readXml($this->getFile());
			$mdt = $ds1->hashTables->get($this->getTableName());
			if (($mdt == NULL))
			{
				$mdt = new DataTable();
			}
			Cash->AddObj($this->getTname(), $Depositary->Application, $mdt);
			return $ds1->hashTables->get($this->getTableName());
		}
		$di = new File($this->getFile());
		if (!$di->exists())
		{
			throw new \RuntimeException(("文件不存�?:" . $this->getFile()));
		}
		$fis = $this->getListFiles($this->getFile(), "xml",  TRUE );
		if (((NULL == $fis) || ($fis->size() == 0)))
		{
			$fis = $this->getListFiles($this->getFile(), "txt",  TRUE );
			return $this->GetTableTxts($fis);
		}
		$dt = new DataTable($this->getTableName());
		if (($fis->size() == 0))
		{
			return $dt;
		}
		$tempDT = new DataTable();
		foreach ($fis as $fi) 		{
			$ds = new DataSet("myds");
			try 
			{
				$ds->readXml(($this->getFile() . $fi->getName()));
			}
			catch (\RuntimeException $ex)
			{
				throw new \RuntimeException(((("读取文件:" . $fi->getName()) . "错误。Exception=") . $ex->getMessage()));
			}
			try 
			{
				if (($dt->Columns->size() == 0))
				{
					try 
					{
						$dt = $ds->hashTables->get($this->getTableName());
					}
					catch (\RuntimeException $ex)
					{
						throw new \RuntimeException(((((("可能是没有在" . $fi->getName()) . "文件中找到表:") . $this->getTableName()) . " exception=") . $ex->getMessage()));
					}
					$tempDT = $dt;
					continue;
				}
				$mydt = $ds->hashTables->get($this->getTableName());
				if (($mydt == NULL))
				{
					throw new \RuntimeException(("无此�?:" . $this->getTableName()));
				}
				if (($mydt->Rows->size() == 0))
				{
					continue;
				}
				foreach ($mydt->Rows as $mydr) 				{
					$dr = $dt->NewRow();
					foreach ($tempDT->Columns as $dc) 					{
						if (($dc->ColumnName->indexOf("_Id") != -1))
						{
							continue;
						}
						try 
						{
							$obj = $mydr->getValue($dc->ColumnName);
							$dr->put($dc->ColumnName, $obj);
						}
						catch (\RuntimeException $ex)
						{
							throw new \RuntimeException(("xml 配置错误，多个文件中的属性不对称�?" . $ex->getMessage()));
						}
					}
					$dt->Rows->add($dr);
				}
			}
			catch (\RuntimeException $ex)
			{
				throw new \RuntimeException(((((("获取数据出现错误:fileName=" . $fi->getName()) . " clasName=") . $this) . " MoreInfo=") . $ex->getMessage()));
			}
		}
		Cash->AddObj($this->getTname(), $Depositary->Application, $dt);
		return $dt;
	}
	public function RetrieveAllFromDBSource () 
	{
		Cash->RemoveObj($this->getTname());
		return $this->RetrieveAll();
	}
	public function RetrieveAll () 
	{
		Log->DebugWriteInfo(("�?始读�?:" . $this));
		$this->clear();
		$tempVar =Cash->GetObj($this, $Depositary->Application);
		$ens = (( (($tempVar instanceof XmlEns)) ? $tempVar : NULL ));
		if (($ens != NULL))
		{
			foreach ($ens as $en) 			{
				$this->Add_XmlEn($en);
			}
			return $ens->size();
		}
		$dt = $this->GetTable();
		foreach ($dt->Rows as $dr) 		{
			$en = $this->getNewEntity();
			$en->setRow(new Row());
			$en->getRow()->LoadDataTable($dt, $dr);
			$this->Add_XmlEn($en);
		}
		Cash->AddObj($this, $Depositary->Application, $this);
		Log->DebugWriteInfo(("读取结束" . $this));
		return $dt->Rows->size();
	}
	public function FullEnToCash ($pk) // [String pk]
	{
		$this->RetrieveAll();
		$myen = $this->getNewEntity();
		foreach ($this as $en) 		{
			$Cash->AddObj(($myen . $en->GetValByKey($pk)), $Depositary->Application, $en);
		}
	}
	public function RetrieveByLength ($key, $len) // [String key, int len]
	{
		$this->clear();
		$dt = $this->GetTable();
		$i = 0;
		foreach ($dt->Rows as $dr) 		{
			if (($dr->getValue($key)->length() == $len))
			{
				$en = $this->getNewEntity();
				$en->setRow(new Row());
				$en->getRow()->LoadDataTable($dt, $dr);
				$this->Add_XmlEn($en);
				++$i;
			}
		}
		return $i;
	}
	public function Retrieve_String_Object ($key, $val) // [String key, Object val]
	{
			/* match: String_Object */
		return $this->RetrieveBy_String_Object($key, $val);
	}
	public function Retrieve_String_Object_String_String ($key, $val, $key1, $val1) // [String key, Object val, String key1, String val1]
	{
		$this->clear();
		$dt = $this->GetTable();
		if (($dt == NULL))
		{
			throw new \RuntimeException((((((("@错误：类" . $this->getNewEntity()) . " File= ") . $this->getFile()) . " Table=") . $this->getTableName()) . " ，没有取到数据�??"));
		}
		$i = 0;
		try 
		{
			foreach ($dt->Rows as $dr) 			{
				if (($dr->getValue($key)->equals($val) && $dr->getValue($key1)->equals($val1)))
				{
					$en = $this->getNewEntity();
					$en->setRow(new Row());
					$en->getRow()->LoadDataTable($dt, $dr);
					$this->Add_XmlEn($en);
					++$i;
				}
			}
		}
		catch (\RuntimeException $ex)
		{
			throw new \RuntimeException(((((("@装载错误:file=" . $this->getFile()) . " xml:") . $this) . "Err:") . $ex->getMessage()));
		}
		return $i;
	}
	public function RetrieveBy_String_Object ($key, $val) // [String key, Object val]
	{
		if (($val == NULL))
		{
			return 0;
		}
		$this->clear();
		$dt = $this->GetTable();
		if (($dt == NULL))
		{
			throw new \RuntimeException((((((("@错误：类" . $this->getNewEntity()) . " File= ") . $this->getFile()) . " Table=") . $this->getTableName()) . " ，没有取到数据�??"));
		}
		$i = 0;
		foreach ($dt->Rows as $dr) 		{
			if ($dr->getValue($key)->equals($val))
			{
				$en = $this->getNewEntity();
				$en->setRow(new Row());
				$en->getRow()->LoadDataTable($dt, $dr);
				$this->Add_XmlEn($en);
				++$i;
			}
		}
		return $i;
	}
	public function RetrieveBy_String_Object_String ($key, $val, $orderByAttr) // [String key, Object val, String orderByAttr]
	{
		$dt = $this->GetTable();
		$this->clear();
		$i = 0;
		foreach ($dt->Rows as $dr) 		{
			if ($dr->getValue($key)->equals($val))
			{
				$en = $this->getNewEntity();
				$en->setRow(new Row());
				$en->getRow()->LoadDataTable($dt, $dr);
				$this->Add_XmlEn($en);
				++$i;
			}
		}
		return $i;
	}
	public function Find ($key, $val) // [String key, Object val]
	{
		foreach ($this as $en) 		{
			if ($val->equals($en->GetValStringByKey($key)))
			{
				return $en;
			}
		}
		return NULL;
	}
	public function IsExits ($key, $val) // [String key, Object val]
	{
		foreach ($this as $en) 		{
			if ($val->equals($en->GetValStringByKey($key)))
			{
				return  TRUE ;
			}
		}
		return  FALSE ;
	}
	public function GetEnByKey ($key, $val) // [String key, String val]
	{
		foreach ($this as $en) 		{
			if ($en->GetValStringByKey($key)->equals($val))
			{
				return $en;
			}
		}
		return NULL;
	}
	public function getItem_I ($index) // [int index]
	{
		return $this->get($index);
	}
	public function getItem_String_String ($key, $val) // [String key, String val]
	{
		foreach ($this as $en) 		{
			if ($en->GetValStringByKey($key)->equals($val))
			{
				return $en;
			}
		}
		throw new \RuntimeException((((((((((("在[" . $this->getTableName()) . ",") . $this->getFile()) . ",") . $this) . "]没有找到key=") . $key) . ", val=") . $val) . "的实例�??"));
	}
	public function Add_XmlEn ($entity) // [XmlEn entity]
	{
		$this->add($entity);
	}
	public function Add_XmlEns ($ens) // [XmlEns ens]
	{
		if (($ens == NULL))
		{
			return 0;
		}
		foreach ($ens as $en) 		{
			$this->add($en);
		}
		return $ens->size();
	}
	public function FillXmlDataIntoEntities_Entities ($ens) // [Entities ens]
	{
		$this->RetrieveAll();
		$en1 = $ens->getNewEntity();
		$pks = $en1->getPKs();
		foreach ($this as $xmlen) 		{
			$en = $ens->getNewEntity();
			foreach ($pks as $pk) 			{
				$obj = $xmlen->GetValByKey($pk);
				$en->SetValByKey($pk, $obj);
			}
			try 
			{
				$en->RetrieveFromDBSources();
			}
			catch (\RuntimeException $ex)
			{
				try 
				{
					$en->CheckPhysicsTable();
				}
				catch (Exception $e)
				{
					$e->printStackTrace();
				}
				$Log->DebugWriteError($ex->getMessage());
			}
			foreach ($xmlen->getRow()->keySet() as $s) 			{
				$obj = $xmlen->GetValByKey($s);
				if (($obj == NULL))
				{
					continue;
				}
				if ($obj->equals(""))
				{
					continue;
				}
				if ($obj->equals("None"))
				{
					continue;
				}
				$en->SetValByKey($s, $obj);
			}
			$en->Save();
		}
	}
	public function FillXmlDataIntoEntities () 
	{
		if (($this->getRefEns() == NULL))
		{
			return ;
		}
			/* match: Entities */
		$this->FillXmlDataIntoEntities_Entities($this->getRefEns());
	}
	abstract function getNewEntity (); 
	abstract function getFile (); 
	abstract function getTableName (); 
	abstract function getRefEns (); 
	public function ToDataTable () 
	{
		$dt = new DataTable($this->getTableName());
		if (($this->size() == 0))
		{
			return $dt;
		}
			/* match: I */
		$tempVar = $this->getItem_I(0);
		$en = ( (($tempVar instanceof XmlEn)) ? $tempVar : NULL );
		$r = $en->getRow();
		foreach ($r->keySet() as $key) 		{
			$dt->Columns->Add($key, String);
		}
		foreach ($this as $en1) 		{
			$dr = $dt->NewRow();
			foreach ($r->keySet() as $key) 			{
				$dr->put($key, $en1->GetValStringByKey($key));
			}
			$dt->Rows->add($dr);
		}
		return $dt;
	}
	protected function getListFiles ($path, $suffix, $isdepth) // [String path, String suffix, boolean isdepth]
	{
		$lstFileNames = [];// 
		$file = new File($path);
		return $this->listFile($lstFileNames, $file, $suffix, $isdepth);
	}
	protected function listFile ($lstFileNames, $f, $suffix, $isdepth) // [List<File> lstFileNames, File f, String suffix, boolean isdepth]
	{
		if ($f->isDirectory())
		{
			$t = $f->listFiles();
			for ($i = 0; ($i < count($t) /*from: t.length*/); ++$i) 
			{
				if (($isdepth || $t[$i]->isFile()))
				{
					$this->listFile($lstFileNames, $t[$i], $suffix, $isdepth);
				}
			}
		}
		else
		{
			$filePath = $f->getAbsolutePath();
			if (!$suffix->equals(""))
			{
				$begIndex = $filePath->lastIndexOf(".");
				$tempsuffix = "";
				if (($begIndex != -1))
				{
					$tempsuffix = $filePath->substring(($begIndex + 1), $filePath->length());
					if ($tempsuffix->equals($suffix))
					{
						$lstFileNames->add($f);
					}
				}
			}
			else
			{
				$lstFileNames->add($f);
			}
		}
		return $lstFileNames;
	}
	public function toString () 
	{
		return $this->getClass()->getName();
	}
}
XmlEns::__staticinit(); // initialize static vars for this class on load
?>
